
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RoomRequestForm.java
 *
 * Created on Dec 16, 2011, 9:28:46 AM
 */

/**
 *
 * @author dabeast23
 */
public class RoomRequestForm extends javax.swing.JFrame {

    /** Creates new form RoomRequestForm */
    public RoomRequestForm(java.awt.Frame parent, boolean modal) {
        setResizable(false);
	setTitle("Hotel Kiosk: Room Request Form");
	setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        initComponents();
        this.setLocationRelativeTo(null);
    }

    public RoomRequestForm(RequestARoom parent, boolean modal, int rowNum,ArrayList reservationList)
    {
        setResizable(false);
	setTitle("Hotel Kiosk: Room Request Form");
	setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        initComponents();
        this.reservationList = reservationList;
        this.setLocationRelativeTo(null);
        floorBox.setSelectedIndex(0);
        floorBox.setEnabled(false);
        roomBox.setSelectedIndex(rowNum);
        roomBox.setEnabled(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        backgroundPanel = new javax.swing.JPanel();
        guestIDPromptLabel = new javax.swing.JLabel();
        firstNameLabel = new javax.swing.JLabel();
        floorLabel = new javax.swing.JLabel();
        roomLabel = new javax.swing.JLabel();
        telNumLabel = new javax.swing.JLabel();
        emailAddLabel = new javax.swing.JLabel();
        lastNameLabel = new javax.swing.JLabel();
        firstName = new javax.swing.JTextField();
        lastName = new javax.swing.JTextField();
        floorBox = new javax.swing.JComboBox();
        roomBox = new javax.swing.JComboBox();
        telNum = new javax.swing.JTextField();
        emailAdd = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        requestButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        backgroundPanel.setBackground(new java.awt.Color(102, 0, 0));

        guestIDPromptLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
        guestIDPromptLabel.setForeground(new java.awt.Color(255, 255, 204));
        guestIDPromptLabel.setText("Room Request");

        firstNameLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 0, 14));
        firstNameLabel.setForeground(new java.awt.Color(255, 255, 204));
        firstNameLabel.setText("First Name:");

        floorLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 0, 14));
        floorLabel.setForeground(new java.awt.Color(255, 255, 204));
        floorLabel.setText("Floor Number:");

        roomLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 0, 14));
        roomLabel.setForeground(new java.awt.Color(255, 255, 204));
        roomLabel.setText("Room NUmber:");

        telNumLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 0, 14));
        telNumLabel.setForeground(new java.awt.Color(255, 255, 204));
        telNumLabel.setText("Telephone NUmber:");

        emailAddLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 0, 14));
        emailAddLabel.setForeground(new java.awt.Color(255, 255, 204));
        emailAddLabel.setText("EMail Address:");

        lastNameLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 0, 14));
        lastNameLabel.setForeground(new java.awt.Color(255, 255, 204));
        lastNameLabel.setText("Last Name:");

        floorBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1" }));

        roomBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114" }));

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        requestButton.setText("Request");
        requestButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                requestButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout backgroundPanelLayout = new javax.swing.GroupLayout(backgroundPanel);
        backgroundPanel.setLayout(backgroundPanelLayout);
        backgroundPanelLayout.setHorizontalGroup(
            backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backgroundPanelLayout.createSequentialGroup()
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(backgroundPanelLayout.createSequentialGroup()
                        .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(backgroundPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(guestIDPromptLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(backgroundPanelLayout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(requestButton)
                                .addGap(18, 18, 18)))
                        .addComponent(cancelButton))
                    .addGroup(backgroundPanelLayout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(firstNameLabel)
                            .addComponent(lastNameLabel)
                            .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(floorLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(roomLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(telNumLabel)
                                .addComponent(emailAddLabel)))
                        .addGap(59, 59, 59)
                        .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(emailAdd)
                            .addComponent(telNum)
                            .addComponent(roomBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(floorBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lastName)
                            .addComponent(firstName, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))))
                .addContainerGap())
        );
        backgroundPanelLayout.setVerticalGroup(
            backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(backgroundPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(guestIDPromptLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(firstNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(firstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lastNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(floorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(floorBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(roomLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(roomBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(telNumLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(telNum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailAddLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(emailAdd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 48, Short.MAX_VALUE)
                .addGroup(backgroundPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(requestButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 418, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(backgroundPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        RequestARoom RAR = new RequestARoom(reservationList);
        RAR.setVisible(true);
        this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void requestButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_requestButtonActionPerformed
        String tel = telNum.getText();
        String emailA = emailAdd.getText();
        if (firstName.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "First Name Field is empty");
            return;
        }
        else if(lastName.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Last Name Field is empty");
            return;
        }
        else if(!isPhoneNumberValid(tel))
        {
            JOptionPane.showMessageDialog(this, "Invalid phone number. Phone Number isn't in an accepted form\n (nnn)nnn-nnnn; nnnnnnnnnn; nnn-nnn-nnnn.");
            return;
        }
        else if(!isEmailAddressValid(emailA))
        {
            JOptionPane.showMessageDialog(this, "Invalid email address");
            return;
        }
        else
        {
            reservation = new Reservation();
            JOptionPane.showMessageDialog(this, "Your room has been successfully requested. Come in and check in :)");
            RequestARoom RAR = new RequestARoom(reservationList);
            RAR.setVisible(true);
            this.setVisible(false);
        }
    }//GEN-LAST:event_requestButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                RoomRequestForm dialog = new RoomRequestForm(new javax.swing.JFrame(), true);
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel backgroundPanel;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField emailAdd;
    private javax.swing.JLabel emailAddLabel;
    private javax.swing.JTextField firstName;
    private javax.swing.JLabel firstNameLabel;
    private javax.swing.JComboBox floorBox;
    private javax.swing.JLabel floorLabel;
    private javax.swing.JLabel guestIDPromptLabel;
    private javax.swing.JTextField lastName;
    private javax.swing.JLabel lastNameLabel;
    private javax.swing.JButton requestButton;
    private javax.swing.JComboBox roomBox;
    private javax.swing.JLabel roomLabel;
    private javax.swing.JTextField telNum;
    private javax.swing.JLabel telNumLabel;
    // End of variables declaration//GEN-END:variables
    private Reservation reservation;
    private int index = 0;
    private ArrayList <Reservation> reservationList;

    public boolean isPhoneNumberValid(String phoneNumber)
    {
        boolean isValid = false;
        /* Phone Number formats: (nnn)nnn-nnnn; nnnnnnnnnn; nnn-nnn-nnnn
            ^\\(?</STRONG> : May start with an option "(" .
            (\\d{3})</STRONG>: Followed by 3 digits.
            \\)?</STRONG> : May have an optional ")"
            [- ]?</STRONG> : May have an optional "-" after the first 3 digits or after optional ) character.
            (\\d{3})</STRONG> : Followed by 3 digits.
             [- ]? </STRONG>: May have another optional "-" after numeric digits.
             (\\d{4})$</STRONG> : ends with four digits.

        */

        String expression = "^\\(?(\\d{3})\\)?[- ]?(\\d{3})[- ]?(\\d{4})$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    public boolean isEmailAddressValid(String emailAddress)
    {
        boolean isValid = false;

        String expression = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
        CharSequence inputStr = emailAddress;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }
}
